package com.sc.web.gwt.client.view.administratorwidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.sc.web.gwt.client.place.LoginWidgetPlace;
import com.sc.web.gwt.client.service.GetUserService;
import com.sc.web.gwt.client.service.GetUserServiceAsync;
import com.sc.web.gwt.client.view.administratorwidget.contents.AdminAccountManagementWidget;
import com.sc.web.gwt.client.view.administratorwidget.contents.ViewAllAgentsWidget;

public class AdministratorWidgetViewImpl extends Composite implements AdministratorWidgetView {
    private static Binder binder = GWT.create(Binder.class);
    private AdministratorWidgetResources resources = GWT.create(AdministratorWidgetResources.class);

    GetUserServiceAsync getUserService = GWT.create(GetUserService.class);

    public interface Binder extends UiBinder<HTMLPanel, AdministratorWidgetViewImpl> {
    }

    private Presenter presenter;

    @UiField
    HorizontalPanel hPanel;

    @UiField
    HTMLPanel leftPanel;

    @UiField
    HTMLPanel rightPanel;

    @UiField
    Anchor viewAllAgents;

    @UiField
    Anchor logout;

    @UiField
    Anchor manageAccount;

    @UiField
    Label welcome;

    @UiField
    Label helpLabel;
    @UiField
    Anchor searchAgent;

    public AdministratorWidgetViewImpl() {
        initWidget(binder.createAndBindUi(this));
        resources.ADMINISTRATOR_WIDGET_CSS().ensureInjected();
        hPanel.setCellWidth(leftPanel, "15%");
        hPanel.setSpacing(5);
        hPanel.setBorderWidth(3);
        getUserService.isUserLoggedIn(new AsyncCallback<String>() {
            public void onFailure(Throwable throwable) {
                throwable.printStackTrace();
            }

            public void onSuccess(String s) {
                if(s == null || s.compareTo("admin") != 0){
                    presenter.goTo(new LoginWidgetPlace());
                }
            }
        });
    }

    public void setPresenter(Presenter presenter) {
        this.presenter = presenter;
    }

    public void setWelcomeText(String welcomeText) {
        helpLabel.setText(welcomeText);
    }

    @UiHandler("searchAgent")
    public void SearchAgentsClickHandler(ClickEvent event) {
        clearRightPanel();
        rightPanel.add(new ViewAllAgentsWidget(false));
    }

    @UiHandler("viewAllAgents")
    public void viewAllUsersClickHandler(ClickEvent event) {
        clearRightPanel();
        rightPanel.add(new ViewAllAgentsWidget(true));
    }

    @UiHandler("manageAccount")
    public void manageAccountClickHandler(ClickEvent event) {
        clearRightPanel();
        rightPanel.add(new AdminAccountManagementWidget());
    }

    @UiHandler("logout")
    public void logoutClickHandler(ClickEvent event) {
        getUserService.logOut(new AsyncCallback<Void>() {
            public void onFailure(Throwable throwable) {
                throwable.printStackTrace();
            }

            public void onSuccess(Void aVoid) {
                presenter.goTo(new LoginWidgetPlace());
            }
        });
    }

    private void clearRightPanel() {
        for (int i = 0; i < rightPanel.getWidgetCount(); i++) {
            rightPanel.remove(i);
        }
    }
}
